package com.example.studentapp.login;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.example.studentapp.R;

import java.util.Locale;

public class LanActivity extends AppCompatActivity {
    ImageView lan_returnIV;
    RadioButton spRBtn,enRBtn,cpRBtn;
    TextView lan_saveTV;
    String lan_select;
    Resources resources;  // 资源
    Configuration configuration; // 配置语言
    DisplayMetrics displayMetrics; // 切换
    SharedPreferences sp;
    // 语言设置
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lan);

        init();
        lan_returnIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        spRBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                lan_select = getString(R.string.login_simple);
                spRBtn.setChecked(true);
            }
        });
        enRBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                lan_select=getString(R.string.login_en);
                enRBtn.setChecked(true);
            }
        });
        cpRBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                lan_select=getString(R.string.login_complex);
                cpRBtn.setChecked(true);
            }
        });
        // 设置语言
        lan_saveTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(lan_select.equals(getString(R.string.login_simple))){
                    configuration.locale = Locale.SIMPLIFIED_CHINESE;

                }else if(lan_select.equals(getString(R.string.login_en))){
                    configuration.locale = Locale.ENGLISH;
                }else{
                    configuration.locale = Locale.TAIWAN;
                }
                sp.edit().putString("lan",lan_select).commit();
                resources.updateConfiguration(configuration,displayMetrics);
                Intent intent = new Intent(LanActivity.this,LoginActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    private void init() {
        lan_returnIV = findViewById(R.id.lan_returnIV);
        spRBtn = findViewById(R.id.spRBtn);
        enRBtn = findViewById(R.id.enRBtn);
        cpRBtn = findViewById(R.id.cpRBtn);
        lan_saveTV = findViewById(R.id.lan_saveTV);
        resources = getResources();
        configuration = resources.getConfiguration();
        displayMetrics = resources.getDisplayMetrics();
        sp = getSharedPreferences("stuass", Context.MODE_PRIVATE);
        lan_select =sp.getString("lan",getString(R.string.login_simple));

        if(lan_select.equals(getString(R.string.login_simple))){
            spRBtn.setChecked(true);
        }else if(lan_select.equals(getString(R.string.login_en))){
            enRBtn.setChecked(true);
        }else{
            cpRBtn.setChecked(true);
        }
    }
}